package com.ueffort.social.platform;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MicrophoneDirection;
import android.provider.SyncStateContract;
import android.util.Log;

import com.ueffort.social.SocialCallbackListener;
import com.ueffort.social.SocialProvider;
import com.ueffort.social.SocialSDK;
import com.ueffort.social.action.AuthAction;
import com.ueffort.social.action.RealAction;
import com.ueffort.social.action.ShareAction;
import com.ueffort.social.action.LeaderboardAction;
import com.ueffort.social.action.AchievementAction;
import com.ueffort.social.action.SocialAction;
import com.ueffort.social.inter.ShareObject;
import com.xiaomi.gamecenter.sdk.MiCommplatform;
import com.xiaomi.gamecenter.sdk.MiErrorCode;
import com.xiaomi.gamecenter.sdk.OnExitListner;
import com.xiaomi.gamecenter.sdk.OnInitProcessListener;
import com.xiaomi.gamecenter.sdk.OnLoginProcessListener;
import com.xiaomi.gamecenter.sdk.OnRealNameVerifyProcessListener;
import com.xiaomi.gamecenter.sdk.RoleData;
import com.xiaomi.gamecenter.sdk.entry.MiAccountInfo;
import com.xiaomi.gamecenter.sdk.entry.MiAppInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MiSocialProvider implements SocialProvider{
    private static MiSocialProvider instance=new MiSocialProvider();

    private String appId;
    private Activity mActivity;

    public static MiSocialProvider Instance(){
        return instance;
    }

    private MiSocialProvider(){
    }

    @Override
    public void Init(String appId, Activity activity, SocialAction action){
        this.appId = appId;
        this.mActivity = activity;
        MiAppInfo appInfo = new MiAppInfo();
        appInfo.setAppId(this.appId);
        appInfo.setAppKey(this.appId);
        MiCommplatform.Init(activity, appInfo);
        MiCommplatform.getInstance().miAppExit(activity, new OnExitListner() {
            @Override
            public void onExit(int code) {
                if (code == MiErrorCode.MI_XIAOMI_EXIT){
                    action.OnLogout(SocialSDK.SocialPlatformType.Mi);
                    // android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        });
    }

    @Override
    public boolean isInstall() {
        return true;
    }

    @Override
    public boolean isShared() {
        return false;
    }
    
    @Override
    public boolean isLeaderboard() {
        return false;
    }
    
    @Override
    public boolean isAchievement() {
        return false;
    }
    
    @Override
    public boolean isReal() {
        return true;
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return false;
    }

    @Override
    public void onCreate(Activity activity) {
        MiCommplatform.getInstance().onMainActivityCreate(activity);
    }

    @Override
    public void onDestroy(Activity activity) {
        MiCommplatform.getInstance().onMainActivityDestory();
    }

    @Override
    public void Authorize(AuthAction action) {
        MiCommplatform.getInstance().miLogin(mActivity,
        				new OnLoginProcessListener()
        {
            @Override
            public void finishLoginProcess(int code , MiAccountInfo arg1)
            {
                switch( code )
                {
                    case MiErrorCode.MI_XIAOMI_PAYMENT_SUCCESS:// 登陆成功
                        //获取用户的登陆后的UID（即用户唯一标识）
                        String uid = arg1.getUid();
         
                        //以下为获取session并校验流程，如果是网络游戏必须校验,(12小时过期)
                        //获取用户的登陆的Session（请参考5.3.3流程校验Session有效性）
                        String session = arg1.getSessionId();

                        //请开发者完成将uid和session提交给开发者自己服务器进行session验证

                        HashMap<String, String> data = new HashMap<>();
                        data.put("code", session);
                        data.put("state", uid);
                        action.onComplete(SocialSDK.SocialPlatformType.Mi, data);
                        break;
                    case MiErrorCode.MI_XIAOMI_PAYMENT_ERROR_LOGIN_FAIL:
                        // 登陆失败
                        break;
                    case MiErrorCode.MI_XIAOMI_PAYMENT_ERROR_CANCEL:
                    // 取消登录
                        break;
                    case MiErrorCode.MI_XIAOMI_PAYMENT_ERROR_ACTION_EXECUTED:	
                        //登录操作正在进行中
                        break;		
                    default:
                        // 登录失败
                        break;
                }
            }
        } );
    }

    @Override
    public void AuthorizeStatus(AuthAction action) {

        action.onCancel(SocialSDK.SocialPlatformType.Mi);
    }

    @Override
    public void UpdateData(Map<String, String> info) {
        RoleData data = new RoleData();
        data.setLevel(info.get("level"));
        data.setRoleId(info.get("id"));
        data.setRoleName(info.get("name"));
        MiCommplatform.getInstance().submitRoleData(mActivity, data);
    }

    @Override
    public void Real(RealAction action) {
        MiCommplatform.getInstance().realNameVerify(mActivity, new OnRealNameVerifyProcessListener() {
            @Override
            public void onSuccess() {
                action.onResult(SocialSDK.SocialPlatformType.Mi);
            }

            @Override
            public void closeProgress() {
                action.onCancel(SocialSDK.SocialPlatformType.Mi);
            }

            @Override
            public void onFailure() {
                action.onError(SocialSDK.SocialPlatformType.Mi, new Exception("fail"));
            }
        });
    }

    @Override
    public void Logout(){

    }
    
    @Override
    public void ShareWithObject(ShareObject share, ShareAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Mi);
    }
    
    @Override
    public void SubmitLeaderboard(String key, int value, LeaderboardAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Mi);
    }
    
    @Override
    public void AddAchievement(String key, int value, AchievementAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Mi);
    }
}
